package com.shqcjd.sinopecdemomobile.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.amap.api.location.AMapLocation;
import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.LatLngBounds;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.maps.model.PolylineOptions;
import com.google.gson.Gson;
import com.shqcjd.sinopecdemomobile.R;
import com.shqcjd.sinopecdemomobile.api.ApiService;
import com.shqcjd.sinopecdemomobile.app.MyApp;
import com.shqcjd.sinopecdemomobile.helper.rxjavahelper.RxObserver;
import com.shqcjd.sinopecdemomobile.helper.rxjavahelper.RxSchedulersHelper;
import com.shqcjd.sinopecdemomobile.model.ResponseData;
import com.shqcjd.sinopecdemomobile.model.pojo.PointInfoBean;
import com.shqcjd.sinopecdemomobile.model.pojo.RouteInfoBean;
import com.shqcjd.sinopecdemomobile.model.pojo.TrajectoryPoint;
import com.shqcjd.sinopecdemomobile.ui.activity.preview.VideosActivity;
import com.shqcjd.sinopecdemomobile.ui.base.BaseActivity;
import com.shqcjd.sinopecdemomobile.ui.base.BasePresenter;
import com.shqcjd.sinopecdemomobile.util.UIUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.disposables.Disposable;

public class TrajectoryDetailsActivity extends BaseActivity {

    @BindView(R.id.iv_toolbar_back)
    ImageView ivToolbarBack;
    @BindView(R.id.tv_toolbar_center)
    TextView tvToolbarCenter;
    @BindView(R.id.mv_route)
    MapView mvRoute;

    private Context mContext;
    private int taskId;

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_trajectory_details;
    }

    @Override
    public void init() {
        super.init();
        taskId = getIntent().getIntExtra("taskId", 0);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initMap(savedInstanceState);
    }

    @Override
    public void initView() {
        mContext = this;
        tvToolbarCenter.setText("轨迹详情");
        getData();
    }

    private void initMap(Bundle savedInstanceState) {
        mvRoute.onCreate(savedInstanceState);
    }

    private void initMapView(List<TrajectoryPoint> patrol_point) {
        List<LatLng> latLngs = new ArrayList<LatLng>();
        if(patrol_point.size()==0){
            return;
        }
        LatLng latLng = new LatLng(Double.parseDouble(patrol_point.get(0).latitude), Double.parseDouble(patrol_point.get(0).longitude));//构造一个位置
//        mvRoute.getMap().moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 15));
        for (int i = 0; i <= patrol_point.size(); i++) {
            if (i == patrol_point.size()) {
                latLngs.add(UIUtils.getGpsChangeLocation(Double.parseDouble(patrol_point.get(0).latitude), Double.parseDouble(patrol_point.get(0).longitude), mContext));
            } else {
                latLngs.add(UIUtils.getGpsChangeLocation(Double.parseDouble(patrol_point.get(i).latitude), Double.parseDouble(patrol_point.get(i).longitude), mContext));
                LatLng latLng2 = UIUtils.getGpsChangeLocation(Double.parseDouble(patrol_point.get(i).latitude), Double.parseDouble(patrol_point.get(i).longitude), mContext);
                Marker marker = mvRoute.getMap().addMarker(new MarkerOptions().position(latLng2).title("玉环").snippet(""));
                marker.setObject(patrol_point.get(i));
                if(i==0){
                    marker.showInfoWindow();
                }
            }
        }
        LatLngBounds.Builder newbounds = new LatLngBounds.Builder();
        for (int i = 0; i < latLngs.size(); i++) {//trajectorylist为轨迹集合
            newbounds.include(latLngs.get(i));
        }
        mvRoute.getMap().animateCamera(CameraUpdateFactory.newLatLngBounds(newbounds.build(),
                150));//第二个参数为四周留空宽度

        mvRoute.getMap().addPolyline(new PolylineOptions().
                addAll(latLngs).width(10).color(Color.argb(255, 239, 49, 49)));
        //画点
        mvRoute.getMap().setOnMarkerClickListener(new AMap.OnMarkerClickListener() {
            @Override
            public boolean onMarkerClick(Marker marker) {
                if (marker.isInfoWindowShown()) {
                    marker.hideInfoWindow();
                } else {
                    marker.showInfoWindow();
                }
                return true;
            }
        });

        //自定义marker样式
        mvRoute.getMap().setInfoWindowAdapter(new AMap.InfoWindowAdapter() {
            @Override
            public View getInfoWindow(Marker marker) {
                TrajectoryPoint trajectoryPoint = (TrajectoryPoint) marker.getObject();
                View infoWindow = LayoutInflater.from(mContext).inflate(
                        R.layout.route_info_map, null);
                ImageView iv_route_info_map = infoWindow.findViewById(R.id.iv_route_info_map);
                TextView tv_route_info_map = infoWindow.findViewById(R.id.tv_route_info_map);
                tv_route_info_map.setText(trajectoryPoint.patrol_point_name);
                iv_route_info_map.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent it = new Intent(mContext, PlaybackActivity.class);
                        it.putExtra("inspectId", trajectoryPoint.id);
                        it.putExtra("tId", taskId);
                        startActivity(it);
                    }
                });
                return infoWindow;
            }

            @Override
            public View getInfoContents(Marker marker) {
                return null;
            }
        });

    }

    private void getData() {
        ApiService.getInspectedPoint(taskId+"")
                .compose(RxSchedulersHelper.io_main())
                .subscribe(new RxObserver<ResponseData<List<TrajectoryPoint>>>() {
                    @Override
                    public void _onSubscribe(Disposable d) {
                        super._onSubscribe(d);
                        showWaitingDialog("正在加载...");
                    }

                    @Override
                    public void _onNext(ResponseData<List<TrajectoryPoint>> responseData) {
                        if (responseData.retCode != 10000) {
                            return;
                        }
                        if (responseData.data.size()<=0) {
                            return;
                        }
                        initMapView(responseData.data);
                    }

                    @Override
                    public void _onError(String errorMessage) {
                        Log.e("tou", errorMessage);
                        hideWaitingDialog();
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                        hideWaitingDialog();
                    }
                });
    }

    @OnClick(R.id.iv_toolbar_back)
    public void onViewClicked() {
        finish();
    }
}
